﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Ivi.Visa.Interop;

namespace libScvs
{
    public class LibScvs
    {
        public FormattedIO488 Load_IO = new FormattedIO488();
        public bool _enaSimulate = false; //用来表示仪表是否运行于offline模式下
        public string mode;

        public string[] Scpis;
        public string currentScpi;
        /// <summary>
        /// 返回参数1对应的连接，返回IDN命令返回值
        /// </summary>
        /// <param name="addr"></param>
        /// <returns></returns>
        public string Scp_open(string addr)
        {
            ResourceManager rm = new ResourceManager();
            //Load_IO = new FormattedIO488();
            string idn = null;
            try
            {
                Load_IO = new Ivi.Visa.Interop.FormattedIO488();

                Load_IO.IO = (IMessage)rm.Open(addr, AccessMode.NO_LOCK, 10000, "");
                Load_IO.IO.Timeout = 5000; 
                Load_IO.WriteString("*IDN?", true); //Get the IDN string
                idn = Load_IO.ReadString();
                Load_IO.WriteString("SYSTem:REMote");
                Thread.Sleep(50);
                return "Load" + idn;
            }
            catch (Exception)
            {
                return "Load Connect Fail"; //连接失败或者仿真的状态，分别返回不同的字符串
            }
        }

        /// <summary>
        /// 关闭连接
        /// </summary>
        public void Scp_close()
        {
            if (!_enaSimulate)
            {
                Load_IO.WriteString("SYSTem:LOCal");
                Thread.Sleep(10);
                Load_IO.IO.Close();
            }
        }

        /// <summary>
        /// 进入指定mode模式
        /// </summary>
        /// <param name="mode">cc/cv/cw/cr</param>
        /// <returns></returns>
        public bool Scp_load_mode(string _mode)
        {
            if (_mode == "CC")
            {
                Load_IO.WriteString("MODE VOLT");//模式设置
                Console.WriteLine($"设置{_mode}模式成功");
                return true;
            }
            else if (_mode == "CV")
            {
                Load_IO.WriteString("MODE CURR");//模式设置
                Console.WriteLine($"设置{_mode}模式成功");
                return true;
            }
            else if (_mode == "CW")
            {
                Load_IO.WriteString("MODE POW");//模式设置
                Console.WriteLine($"设置{_mode}模式成功");
                return true;
            }
            else if (_mode == "CR")
            {
                Load_IO.WriteString("MODE RES");//模式设置
                Console.WriteLine($"设置{_mode}模式成功");
                return true;
            }
            else
            {
                Console.WriteLine($"设置{_mode}模式失败");
                return false;
            }
        }

        /// <summary>
        ///设置当前模式保护值，返回设置成功的保护值 
        /// </summary>
        /// <param name="Curr_Prot"></param>
        /// <returns></returns>
        public float Scp_load_prot(float Curr_Prot)
        {
            Load_IO.WriteString("CURR:PORT" + Curr_Prot);
            return Curr_Prot;
        }

        /// <summary>
        ///设置当前模式设置值，返回设置成功的设置值 
        /// </summary>
        /// <param name="Power_Port"></param>
        /// <returns></returns>
        public float Scp_load_val(float Power_Port)
        {
            Load_IO.WriteString("OUTP:CURR" + Power_Port);
            return Power_Port;
        }

        /// <summary>
        /// 设置电子负载开关打开或关闭
        /// 打开value为true，关闭value为false
        /// </summary>
        /// <param name="value">打开value为true，关闭value为false</param>
        /// <returns></returns>
        public bool Scp_load_ctrl(bool value)
        {
            if (value == true)
            {
                Load_IO.WriteString("INP 1");
                return true;
            }
            else
            {
                Load_IO.WriteString("INP 0");
                return false;
            }

        }

        /// <summary>
        /// 切换到对应的连接上，参数对应scp_open返回值
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool Scp_load_sel(string value)
        {
            Scp_open(value);
            Console.WriteLine($"已切换到{value}");
            return true;
        }

        /// <summary>
        /// true代表返回当前电压值，false代表返回当前电流值
        /// </summary>
        /// <param name="isMead"></param>
        /// <returns></returns>
        public float Scp_load_meas(bool isMead)
        {
            float[] output = new float[2];

            if (isMead) 
            {
                //返回电压值    
                Load_IO.WriteString("MEAS:VOLT?");
                Thread.Sleep(200);
                output[0] = (float)Convert.ToDouble(Load_IO.ReadString()) * 1000;
                return output[0];
            }
            else
            {
                //返回电流值
                Load_IO.WriteString("MEAS:CURR?");
                Thread.Sleep(200);
                output[1] = (float)Convert.ToDouble(Load_IO.ReadString()) * 1000;
                return output[1];
            }
        }
    }
}
